/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pruebaMad.java
 *
 * Created on 27-ene-2009, 12:10:43
 */
package edu.upf.grupE.main;

import edu.upf.grupE.Entities.GeoLocalization;
import edu.upf.grupE.Entities.Picture;
import edu.upf.grupE.Entities.Tag;
import edu.upf.grupE.Entities.TextDescription;
import edu.upf.grupE.impl.Config;
import edu.upf.grupE.impl.IM;

/**
 *
 * @author Fernando Lavilla
 */
public class IlusioGui extends javax.swing.JFrame {
    //Variable declaration

    private IM instantmanager;
    private boolean conectado;

    /** Creates new form pruebaMad. */
    public IlusioGui() {
        initComponents();
        //instantmanager = new IM(connectionTextArea);
        conectado = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        destinationTextField = new javax.swing.JTextField();
        destinationLabel = new javax.swing.JLabel();
        msgTextField = new javax.swing.JTextField();
        messageLabel = new javax.swing.JLabel();
        sendButton = new javax.swing.JButton();
        connectionLabel = new javax.swing.JLabel();
        objectLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        connectionTextArea = new javax.swing.JTextArea();
        ontologyTextField = new javax.swing.JTextField();
        ontologyLabel = new javax.swing.JLabel();
        tagLabel = new javax.swing.JLabel();
        tagTextField = new javax.swing.JTextField();
        searchButton = new javax.swing.JButton();
        user = new javax.swing.JTextField();
        pass = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        destinationLabel.setText("Destiny");

        messageLabel.setText("Message");

        sendButton.setText("Send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        connectionLabel.setText("Connection Data");

        objectLabel.setText("Object Data");

        connectionTextArea.setColumns(20);
        connectionTextArea.setRows(5);
        jScrollPane1.setViewportView(connectionTextArea);

        ontologyTextField.setText("lugar");

        ontologyLabel.setText("Ontology");

        tagLabel.setText("Tag");

        tagTextField.setText("barcelona");

        searchButton.setText("Send Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        user.setText("user");
        user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userActionPerformed(evt);
            }
        });

        Login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stock_connect.png")));
        Login.setText("Login");
        Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginActionPerformed(evt);
            }
        });

        pass.setText("pass");
        pass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Login)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(39, 39, 39)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(destinationLabel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(messageLabel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(msgTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                                            .addComponent(destinationTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(connectionLabel)
                                .addGap(509, 509, 509)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(tagLabel)
                                        .addComponent(ontologyLabel))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(20, 20, 20)
                                            .addComponent(objectLabel)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 286, Short.MAX_VALUE))
                                        .addComponent(tagTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                                        .addComponent(ontologyTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                                    .addGap(105, 105, 105))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(201, 201, 201)
                                    .addComponent(searchButton)
                                    .addContainerGap()))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(sendButton)
                                .addContainerGap())))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Login))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connectionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(objectLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(destinationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(destinationLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ontologyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ontologyLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tagLabel)
                            .addComponent(tagTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(73, 73, 73)
                        .addComponent(searchButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(msgTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(messageLabel)
                    .addComponent(sendButton))
                .addContainerGap(114, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_userActionPerformed

    private void LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginActionPerformed
        /*  This method implements the login/logout button functions.
        First of all we check if the user is already connected to gmail or not
        by using the boolean variable "conectado"
        If the user is connected, it means that he has clicked the logout button
        so the function will logout him and change the button text to login.
        else, if the user is not connecter, this method gets the login and the password
        from the text field and use them to connect to gtalk. It also creates three
        tipes of filters to distinguish the packets we are sending in three types: PacketChatID,
        PacketSearchProfileID and PacketSearchResultID. It will be useful because we will have
        three listeners which will recieve each of those packets separately and we will
        be able to work with them in different ways.    */
        if (conectado) {
            instantmanager.logout();
            Login.setText("Login");
            Login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stock_connect.png")));
            conectado = false;
        } else {
            String login = user.getText();
            String password = new String(pass.getPassword());
            instantmanager.login(login, password, Config.RESOURCE);
            instantmanager.changePresence(Config.PRECENSE);
            //instantmanager.createAndJoinRoom(user.getText());
            conectado = true;
            Login.setText("Logout");
            Login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stock_disconnect.png")));
        }
}//GEN-LAST:event_LoginActionPerformed

    /**
     *This method prints a String in the connectionTextArea square
     * @param text
     */
    public void addText(String text) {
        connectionTextArea.append(text + "\n");
    }

    private void passActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_passActionPerformed

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        //This method is just done to get the values of the chat message and the @
        //introduced into de GUI and pass those strings to the the sendChat method
        //which will put it into a packet and send it to the corresponding address

        instantmanager.sendChat(destinationTextField.getText(), msgTextField.getText());
    }//GEN-LAST:event_sendButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        /* This method creates a Picture class and fills it with the data we write on
        the gui. After this, the method calls the method which will put the object in a packet and send
        the Picture object to the destination URL writen on the destinationTextField field.*/


        Picture searchPic = new Picture();
        TextDescription txdesc = new TextDescription();
        Tag tag = new Tag();
        tag.setContent(tagTextField.getText());
        tag.setOntology(ontologyTextField.getText());
        txdesc.addTag(tag);
        searchPic.setTextDescription(txdesc);
        
        /*
        System.out.println("VALORES DE searchPic:");
        System.out.println("AUTHOR: " + searchPic.getAuthor(0));
        System.out.println("KEYWORD: " + searchPic.getKeyword(0));
        System.out.println("TAG: " + searchPic.getTag(0));
        System.out.println("LATITUDE: " + searchPic.getLatitude());
        System.out.println("LONGITUDE: " + searchPic.getLongitude());*/

        //instantmanager.sendSearchProfile(searchPic);


    }//GEN-LAST:event_searchButtonActionPerformed

    /**A thread is created to implement the logic of the application.
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new IlusioGui().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private final javax.swing.JButton Login = new javax.swing.JButton();
    private javax.swing.JLabel connectionLabel;
    private javax.swing.JTextArea connectionTextArea;
    private javax.swing.JLabel destinationLabel;
    private javax.swing.JTextField destinationTextField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JTextField msgTextField;
    private javax.swing.JLabel objectLabel;
    private javax.swing.JLabel ontologyLabel;
    private javax.swing.JTextField ontologyTextField;
    private javax.swing.JPasswordField pass;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton sendButton;
    private javax.swing.JLabel tagLabel;
    private javax.swing.JTextField tagTextField;
    private javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables
}
